package com.linln.admin.shop.controller;

import com.linln.common.enums.ShopEnum;
import com.linln.common.vo.JsonResult;
import com.linln.common.vo.PageResult;
import com.linln.modules.shopsService.domain.Shops;
import com.linln.modules.shopsService.service.ShopReviewedService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.admin.shop.reviewed
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.admin.shop.reviewed
 * @Description: TODO
 * @date Date : 2020年12月09日 14:25
 */
@Controller
@RequestMapping("/shopReviewed")
public class ShopReviewedController {
    @GetMapping("/index")
    public String index() {
        return "/shopReviewed/index";
    }

    @Autowired
    private ShopReviewedService shopReviewedService;

    @GetMapping("/getWaitReviewed")
    @ResponseBody
    public Map<String, Object> getWaitReviewed(@RequestParam(required = false, defaultValue = "0") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit
            , String searchName) {
        return getLayuiShopByStatus(page, limit, ShopEnum.UNDERREVIEW.getCode(), searchName);
    }

    @GetMapping("/getReviewedAudit")
    @ResponseBody
    public Map<String, Object> getReviewedAudit(@RequestParam(required = false, defaultValue = "0") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit
            , String searchName) {
        return getLayuiShopByStatus(page, limit, ShopEnum.EXAMINATIONPASSED.getCode(), searchName);
    }

    @GetMapping("/getReviewedFail")
    @ResponseBody
    public Map<String, Object> getReviewedFail(@RequestParam(required = false, defaultValue = "0") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit
            , String searchName) {
        return getLayuiShopByStatus(page, limit, ShopEnum.AUDITNOTPASSED.getCode(), searchName);
    }

    private Map<String, Object> getLayuiShopByStatus(Integer page, Integer limit, Integer status, String searchName) {
        Map<String, Object> shopsByStatus = new HashMap<>();
        try {
            shopsByStatus = shopReviewedService.getShopsByStatus(page, limit, status, searchName);
            shopsByStatus.put("code", 0);
        } catch (Exception e) {
            shopsByStatus.put("code", 500);
            shopsByStatus.put("msg", "获取店铺列表失败");
        }
        return shopsByStatus;
    }


    /**
     * 审核
     *
     * @param shopId              店铺id
     * @param reviewedStatus      审核状态 true 通过 false 拒绝
     * @param reviewedOpinionText 审核意见
     * @return
     */
    @PostMapping("/reviewed")
    @ResponseBody
    public JsonResult reviewed(@RequestParam("shopId") String shopId, @RequestParam("reviewedStatus") Boolean reviewedStatus, String reviewedOpinionText) {
        if (shopReviewedService.reviewed(shopId, reviewedStatus ? 1 : 2, reviewedOpinionText)) {
            return JsonResult.ok("保存成功");
        }
        return JsonResult.error("保存失败");
    }

    @GetMapping("/getUpdateShopAudit")
    @ResponseBody
    public PageResult<Shops> getUpdateShopAudit(@RequestParam(required = false, defaultValue = "1") Integer page, @RequestParam(required = false, defaultValue = "10") Integer limit
            , String searchName) throws JSONException, UnsupportedEncodingException {
        List<Shops> shopsList = shopReviewedService.getUpdateShopAudit(searchName);
        if((page)*limit>shopsList.size()){
            limit = shopsList.size();
        }
        PageResult<Shops> pageResult = new PageResult<>();
        pageResult.setCount(shopsList.size());
        pageResult.setData(shopsList.subList((page-1)*limit,limit));
        return pageResult;
    }

    @PostMapping("/reviewedUpdate")
    @ResponseBody
    public JsonResult reviewedUpdate(String shopId,boolean status){
        try {
            shopReviewedService.reviewedUpdate(shopId, status);
            return JsonResult.ok("保存成功");
        }catch (Exception e){
            e.printStackTrace();
        }
        return JsonResult.error("保存失败");
    }
}
